<template lang="">
    <div style="width:40%;">
         <h2>添加单位</h2>
        <el-divider></el-divider>
        <el-form :model="form" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
            <el-form-item label="单位编号" prop="unitCode">
            <el-input v-model="form.unitCode" readonly ></el-input>
            </el-form-item>
            <el-form-item label="单位名称" prop="unitName">
            <el-input v-model="form.unitName" ></el-input>
            </el-form-item>
            <el-form-item label="备注" prop="remarks">
            <el-input v-model="form.remarks"></el-input>
            </el-form-item>
            <el-form-item>
            <el-button type="primary" @click="submitForm('ruleForm')">添加</el-button>
            <el-button @click="Cancel">返回</el-button>
            </el-form-item>
        </el-form>
    </div>
</template>
<script>
import { AddUnit } from "@/api/BasicsDatumServer/unitServer";
import { autoGenerateCode } from "@/utils/utils";
export default {
    //数据
    data() {
        return {
            form: {
                unitCode: "",
                unitName: "",
                remarks: "",
            },
            rules: {
                unitCode: [
                    { required: true, message: '请输入单位编号', trigger: 'blur' },
                ],
                unitName: [
                    { required: true, message: '请输入单位名称', trigger: 'blur' },
                ],
            },
        }
    },
    //方法
    methods: {
        Cancel() {
            this.$router.push("/BasicsDatumServer/unitMangement/UnitList");
        },
        //添加
        submitForm(formName) {
            this.$refs[formName].validate((valid) => {
                if (valid) {
                    AddUnit(this.form).then((res) => {
                        if (res.data.code == 0) {
                            this.$message({
                                message: '添加成功',
                                type: 'success'
                            });
                            this.$router.push("/BasicsDatumServer/unitMangement/UnitList");
                        } else {
                            this.$message.error('单位名称已存在,请更改');
                        }
                    })
                } else {
                    console.log('error submit!!');
                    return false;
                }
            });
        },
        resetForm(formName) {
            this.$refs[formName].resetFields();
        },
    },
    //挂载
    mounted() {
        this.form.unitCode = autoGenerateCode("DW");
    },
}
</script>
<style lang="">
    
</style>